/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.Server;

import Exceptions.IncompatibleVectorSizesException;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import vectors.VectorIn;
import vectors.Vectors;

/**
 *
 * @author Кирилл
 */
public class Server {

    private static ServerSocket listenSocket;

    public static void main(String[] args) throws IOException {
        try {
            System.out.println("Waiting...");
            int serverPort = 7777;
            listenSocket = new ServerSocket(serverPort);
            while (true) {
                Socket clientSocket = listenSocket.accept();

                System.out.println("New Client ");
                Connection c = new Connection(clientSocket);


            }
        } catch (IOException e) {
            System.out.println("Listen socket:" + e.getMessage());

        }

    }

    private static class Connection extends Thread {

        InputStream in;
        OutputStream out;
        Socket clientSocket;
        VectorIn vc1;
        VectorIn vc2;
        VectorIn vectorResult;

        public Connection(Socket aClientSocket) {
            try {
                clientSocket = aClientSocket;
                in = clientSocket.getInputStream();
                out = clientSocket.getOutputStream();
                vc1 = Vectors.inputVector(in);
                System.out.println(vc1.toString());
                vc2 = Vectors.inputVector(in);
                System.out.println(vc2.toString());

                this.start();
            } catch (IOException e) {
                System.out.println("Connection:" + e.getMessage());
            }
        }

        public void run() {

            try {

                vectorResult = Vectors.vectorPlusVector(vc1, vc2);
                System.out.println(vectorResult);
                Vectors.outputVector(vectorResult, out);

            } catch (IncompatibleVectorSizesException ex) {
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
    }
}